package com.hencoder.hencoderpracticedraw1.practice;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

public class Practice10HistogramView extends View {

    private Paint paint=new Paint();
    private float lineBottom=500f;
    private float mWidth=100;

    private float rectStopRight=120;
    public Practice10HistogramView(Context context) {
        super(context);
    }

    public Practice10HistogramView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public Practice10HistogramView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        rectStopRight=120;

//        综合练习
//        练习内容：使用各种 Canvas.drawXXX() 方法画直方图
        paint.setStyle(Paint.Style.STROKE);
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(3);

        Path path=new Path();
        path.moveTo(100,20);
        path.lineTo(100,lineBottom);
        path.rLineTo(900,0);
        canvas.drawPath(path,paint);


        drawRectAndText(canvas,5,"Froyo");

        drawRectAndText(canvas,20,"GB");

        drawRectAndText(canvas,20,"ICS");

        paint.setTextSize(40);
        paint.setColor(Color.WHITE);
        canvas.drawText("直方图",rectStopRight+10,lineBottom+100,paint);

        drawRectAndText(canvas,180,"JB");

        drawRectAndText(canvas,200,"KitKat");



        drawRectAndText(canvas,300,"L");

        drawRectAndText(canvas,160,"M");



    }


    private void drawFroyo(Canvas canvas){
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.GREEN);

        canvas.drawRect(rectStopRight,lineBottom-10,rectStopRight+mWidth,lineBottom,paint);
        paint.setTextSize(30);
        paint.setColor(Color.YELLOW);
        canvas.drawText("Froyo",rectStopRight+10,lineBottom+30,paint);
        rectStopRight=rectStopRight+mWidth;
    }


    private void drawGB(Canvas canvas){
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.GREEN);
        rectStopRight+=20;
        canvas.drawRect(rectStopRight,lineBottom-50,rectStopRight+mWidth,lineBottom,paint);

        paint.setTextSize(30);
        paint.setColor(Color.YELLOW);
        canvas.drawText("Gb",rectStopRight+10,lineBottom+30,paint);
        rectStopRight=rectStopRight+mWidth;
    }

    /**
     * 画矩形以及文字
     * @param canvas
     * @param height
     * @param text
     */
    private void drawRectAndText(Canvas canvas,float height,String text){
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.GREEN);
        rectStopRight+=20;
        canvas.drawRect(rectStopRight,lineBottom-height,rectStopRight+mWidth,lineBottom,paint);

        paint.setTextSize(30);
        paint.setColor(Color.YELLOW);
        canvas.drawText(text,rectStopRight+10,lineBottom+30,paint);
        rectStopRight=rectStopRight+mWidth;
    }
}
